﻿using System;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Data.SqlClient;
using System.Web.Hosting;
using Microsoft.Web.Administration;

namespace IISLogAnalyzer.Web.IISLoggingModule
{
    public class SqlLoggingModule : IHttpModule
    {
        private string _connectionString;

        public SqlLoggingModule()
        {
            
        }

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            try
            {
                _connectionString = GetConnectionString(context.Context);
            }
            catch (Exception)
            {                
                //throw new Exception("Logging database connection string is missing. Please, check your web.config");
                _connectionString = "server=localhost\\sqlexpress2008;database=aspnetdb;Integrated Security=SSPI;";
            }

            context.LogRequest += new EventHandler(context_LogRequest);
        }

        void context_LogRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            LogRequest(application.Context);
        }

        private void LogRequest(HttpContext httpContext)
        {
            //using (WebLogDataContext dc = new WebLogDataContext(_connectionString))
            //{
            //    W3CLogging entity = new W3CLogging();
            //    entity.time = DateTime.Now;
            //    entity.date = DateTime.Today;
            //    entity.id = Guid.NewGuid();
            //    entity.c_ip = httpContext.Request.UserHostAddress;
            //    entity.cs_method = httpContext.Request.HttpMethod;
            //    entity.cs_ua = httpContext.Request.UserAgent;
            //    entity.cs_uri_query = httpContext.Request.Url.Query;
            //    entity.cs_uri_stem = httpContext.Request.Path;
            //    entity.cs_username = httpContext.Request.ServerVariables["LOGON_USER"];
            //    entity.s_ip = httpContext.Request.ServerVariables["SERVER_NAME"];
            //    entity.s_port = System.Convert.ToInt16(httpContext.Request.ServerVariables["SERVER_PORT"]);
            //    entity.sc_status = System.Convert.ToInt16(httpContext.Response.StatusCode);
            //    entity.sc_substatus = System.Convert.ToInt16(httpContext.Response.SubStatusCode);
            //    entity.sc_win32_status = 0; // not in httpContext
            //    entity.s_referer = httpContext.Request.ServerVariables["HTTP_REFERER"];

            //    dc.W3CLoggings.InsertOnSubmit(entity);
            //    dc.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);  
            //    // ContinueOnConflict so that application will not stop working
            //}
        }

        private string GetConnectionString(HttpContext httpContext)
        {
            ConfigurationSection section = (ConfigurationSection)Microsoft.Web.Administration.WebConfigurationManager.GetSection("system.webServer/sqlLogging");
            return (string)section["connectionString"];
        } 
    }
}
